<!DOCTYPE html>
<html lang="en">
    <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="format-detection" content="telephone=no" />

  <title>
    40|案例篇：网络请求延迟变大了，我该怎么办？ | 迪克猪的博客
  </title>

  
  <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
  <link rel="manifest" href="/manifest.json" />
  <meta name="theme-color" content="#ffffff" />

  
  <link
    rel="stylesheet"
    href="https://unpkg.com/modern-normalize@0.6.0/modern-normalize.css"
  />

  
  
  
  
  <link rel="stylesheet" href="https://zsy619.github.io/style.min.f7761d111b74dd5c07f0111decee92938c12abc42e0fd319e1a07483e248b54e.css" integrity="sha256-93YdERt03VwH8BEd7O6Sk4wSq8QuD9MZ4aB0g&#43;JItU4=" />

  
  
    
  
</head>

    <body>
        <header id="header">
  <div class="header_container">
    <h1 class="sitetitle">
      <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a>
    </h1>
    <nav class="navbar">
      <ul>
        <li><a href="https://zsy619.github.io">Home</a></li>
        
          <li>
            <a href="/post/">
              
              <span>LINUX性能优化</span>
            </a>
          </li>
        
          <li>
            <a href="/csapp/">
              
              <span>深入理解计算机系统_第三版</span>
            </a>
          </li>
        
          <li>
            <a href="/golang/">
              
              <span>golang</span>
            </a>
          </li>
        
          <li>
            <a href="/docker/">
              
              <span>容器</span>
            </a>
          </li>
        
          <li>
            <a href="/flutter/">
              
              <span>Flutter</span>
            </a>
          </li>
        
          <li>
            <a href="/know/">
              
              <span>知识点</span>
            </a>
          </li>
        
          <li>
            <a href="/categories/">
              
              <span>目录</span>
            </a>
          </li>
        
          <li>
            <a href="/about/">
              
              <span>关于</span>
            </a>
          </li>
        
        <li class="hide-sm"><a href="https://zsy619.github.io/index.xml" type="application/rss+xml">RSS</a></li>
      </ul>
    </nav>
  </div>
</header>

        
<section id="main">
  <article class="post content">
    <h2 class="title">40|案例篇：网络请求延迟变大了，我该怎么办？</h2>
    <div class="post_content">
      <p>除了 DDoS 会带来网络延迟增大外，我想，你肯定见到过不少其他原因导致的网络延迟，比如</p>
<ul>
<li>网络传输慢，导致延迟；</li>
<li>Linux 内核协议栈报文处理慢，导致延迟；</li>
<li>应用程序数据处理慢，导致延迟等等。</li>
</ul>
<h3 id="网络延迟">网络延迟</h3>
<p>网络数据传输所用的时间。不过要注意，这个时间可能是单向的，指从源地址发送到目的地址的单程时间；也可能是双向的，即从源地址发送到目的地址，然后又从目的地址发回响应，这个往返全程所用的时间。</p>
<p>通常，我们更常用的是双向的往返通信延迟，比如 ping 测试的结果，就是往返延时 RTT（Round-Trip Time）。</p>
<p>应用程序延迟，它是指，从应用程序接收到请求，再到发回响应，全程所用的时间。通常，应用程序延迟也指的是往返延迟，是网络数据传输时间加上数据处理时间的和。</p>
<p>可以用 ping 来测试网络延迟。ping 基于 ICMP 协议，它通过计算 ICMP 回显响应报文与 ICMP 回显请求报文的时间差，来获得往返延时。这个过程并不需要特殊认证，常被很多网络攻击利用，比如端口扫描工具 nmap、组包工具 hping3 等等。</p>
<p>为了避免这些问题，很多网络服务会把 ICMP 禁止掉，这也就导致我们无法用 ping ，来测试网络服务的可用性和往返延时。这时，你可以用 traceroute 或 hping3 的 TCP 和 UDP 模式，来获取网络延迟。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">
<span style="color:#75715e"># -c表示发送3次请求，-S表示设置TCP SYN，-p表示端口号为80</span>
$ hping3 -c <span style="color:#ae81ff">3</span> -S -p <span style="color:#ae81ff">80</span> baidu.com
HPING baidu.com <span style="color:#f92672">(</span>eth0 123.125.115.110<span style="color:#f92672">)</span>: S set, <span style="color:#ae81ff">40</span> headers + <span style="color:#ae81ff">0</span> data bytes
len<span style="color:#f92672">=</span><span style="color:#ae81ff">46</span> ip<span style="color:#f92672">=</span>123.125.115.110 ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">51</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">47908</span> sport<span style="color:#f92672">=</span><span style="color:#ae81ff">80</span> flags<span style="color:#f92672">=</span>SA seq<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span> win<span style="color:#f92672">=</span><span style="color:#ae81ff">8192</span> rtt<span style="color:#f92672">=</span>20.9 ms
len<span style="color:#f92672">=</span><span style="color:#ae81ff">46</span> ip<span style="color:#f92672">=</span>123.125.115.110 ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">51</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">6788</span>  sport<span style="color:#f92672">=</span><span style="color:#ae81ff">80</span> flags<span style="color:#f92672">=</span>SA seq<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span> win<span style="color:#f92672">=</span><span style="color:#ae81ff">8192</span> rtt<span style="color:#f92672">=</span>20.9 ms
len<span style="color:#f92672">=</span><span style="color:#ae81ff">46</span> ip<span style="color:#f92672">=</span>123.125.115.110 ttl<span style="color:#f92672">=</span><span style="color:#ae81ff">51</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">37699</span> sport<span style="color:#f92672">=</span><span style="color:#ae81ff">80</span> flags<span style="color:#f92672">=</span>SA seq<span style="color:#f92672">=</span><span style="color:#ae81ff">2</span> win<span style="color:#f92672">=</span><span style="color:#ae81ff">8192</span> rtt<span style="color:#f92672">=</span>20.9 ms

--- baidu.com hping statistic ---
<span style="color:#ae81ff">3</span> packets transmitted, <span style="color:#ae81ff">3</span> packets received, 0% packet loss
round-trip min/avg/max <span style="color:#f92672">=</span> 20.9/20.9/20.9 ms

</code></pre></div><p>从 hping3 的结果中，你可以看到，往返延迟 RTT 为 20.9ms。</p>
<p>用 traceroute ，也可以得到类似结果：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#75715e"># --tcp表示使用TCP协议，-p表示端口号，-n表示不对结果中的IP地址执行反向域名解析</span>
$ traceroute --tcp -p <span style="color:#ae81ff">80</span> -n baidu.com
traceroute to baidu.com <span style="color:#f92672">(</span>123.125.115.110<span style="color:#f92672">)</span>, <span style="color:#ae81ff">30</span> hops max, <span style="color:#ae81ff">60</span> byte packets
 <span style="color:#ae81ff">1</span>  * * *
 <span style="color:#ae81ff">2</span>  * * *
 <span style="color:#ae81ff">3</span>  * * *
 <span style="color:#ae81ff">4</span>  * * *
 <span style="color:#ae81ff">5</span>  * * *
 <span style="color:#ae81ff">6</span>  * * *
 <span style="color:#ae81ff">7</span>  * * *
 <span style="color:#ae81ff">8</span>  * * *
 <span style="color:#ae81ff">9</span>  * * *
<span style="color:#ae81ff">10</span>  * * *
<span style="color:#ae81ff">11</span>  * * *
<span style="color:#ae81ff">12</span>  * * *
<span style="color:#ae81ff">13</span>  * * *
<span style="color:#ae81ff">14</span>  123.125.115.110  20.684 ms *  20.798 ms
</code></pre></div><p>traceroute 会在路由的每一跳发送三个包，并在收到响应后，输出往返延时。如果无响应或者响应超时（默认 5s），就会输出一个星号。</p>
<h3 id="小结">小结</h3>
<p>在发现网络延迟增大后，你可以用 traceroute、hping3、tcpdump、Wireshark、strace 等多种工具，来定位网络中的潜在问题。比如，</p>
<ul>
<li>使用 hping3 以及 wrk 等工具，确认单次请求和并发请求情况的网络延迟是否正常。</li>
<li>使用 traceroute，确认路由是否正确，并查看路由中每一跳网关的延迟。</li>
<li>使用 tcpdump 和 Wireshark，确认网络包的收发是否正常。</li>
<li>使用 strace 等，观察应用程序对网络套接字的调用情况是否正常。</li>
</ul>

    </div>
    <div class="info post_meta">
      <time datetime=2020-07-07T16:18:24&#43;0800 class="date">Tuesday, July 7, 2020</time>
      
        <ul class="tags">
        
          <li> <a href="https://zsy619.github.io/tags/linux%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96">linux性能优化</a> </li>
        
          <li> <a href="https://zsy619.github.io/tags/linux">linux</a> </li>
        
        </ul>
      
      
    </div>
    <div class="clearfix"></div>
  </article>
  
    <div class="other_posts">
      
      <a href="https://zsy619.github.io/post/39%E6%A1%88%E4%BE%8B%E7%AF%87%E6%80%8E%E4%B9%88%E7%BC%93%E8%A7%A3-ddos-%E6%94%BB%E5%87%BB%E5%B8%A6%E6%9D%A5%E7%9A%84%E6%80%A7%E8%83%BD%E4%B8%8B%E9%99%8D%E9%97%AE%E9%A2%98/" class="prev">39|案例篇：怎么缓解 DDoS 攻击带来的性能下降问题？</a>
      
      
      <a href="https://zsy619.github.io/post/41%E6%A1%88%E4%BE%8B%E7%AF%87%E5%A6%82%E4%BD%95%E4%BC%98%E5%8C%96-nat-%E6%80%A7%E8%83%BD%E4%B8%8A/" class="next">41|案例篇：如何优化 NAT 性能？（上）</a>
      
    </div>
    <aside id="comments">
</aside>

  
</section>

        <a id="back_to_top" title="Go To Top" href="#">
  <span>
    <svg viewBox="0 0 24 24">
      <path fill="none" d="M0 0h24v24H0z"></path>
      <path d="M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z"></path>
    </svg>
  </span>
</a>

        <footer id="footer">
  <p>
    <span>&copy; 2021 <a href="https://zsy619.github.io" title="迪克猪的博客">迪克猪的博客</a> </span>
    <span>Built with <a rel="nofollow" target="_blank" href="https://gohugo.io">Hugo</a></span>
    <span>Theme by <a rel="nofollow" target="_blank" href="https://github.com/wayjam/hugo-theme-mixedpaper">WayJam</a></span>
  </p>

  <script src="https://zsy619.github.io/js/main.min.8b182175f5874aeed0acc0979345c98d4bde22208ec4f36cc1d6e3102acb4b10.js" integrity="sha256-ixghdfWHSu7QrMCXk0XJjUveIiCOxPNswdbjECrLSxA=" crossorigin="anonymous" async></script>
</footer>

    </body>
</html>
